package com.swift.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

public class GlideRoundWithTextTransform extends BitmapTransformation {

    private static float radius = 0f;
    private static int textColor = 0;
    private static float textSize = 0;
    private static String text = "";

    public GlideRoundWithTextTransform(Context context, int dp,int textColor,float textSize,String text) {
        super(context);
        GlideRoundWithTextTransform.radius = Resources.getSystem().getDisplayMetrics().density * dp;
        GlideRoundWithTextTransform.textColor = textColor;
        GlideRoundWithTextTransform.textSize = textSize;
        GlideRoundWithTextTransform.text = text;
    }

    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
        
        Canvas canvas = new Canvas(result);
        
        // 画出圆角矩形
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        
        // 画出带背景色矩形
        Paint mpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		
		Rect targetRect = new Rect(0, source.getHeight()/4 * 3, source.getWidth(),source.getHeight()); 
		
		mpaint.setColor(textColor);
		mpaint.setStyle(Style.FILL);
		mpaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        
        canvas.drawRect(targetRect, mpaint);
        
        // 在背景色矩形里面画出文字
        mpaint.setTextSize(textSize);
		FontMetricsInt fontMetrics = mpaint.getFontMetricsInt();
		int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
		mpaint.setTextAlign(Paint.Align.CENTER);
		mpaint.setColor(Color.WHITE);
		
		canvas.drawText(text, targetRect.centerX(), baseline, mpaint);
		
        return result;
    }

    @Override public String getId() {
        return getClass().getName() + Math.round(radius);
    }
}
